<template>
  <div>
    <van-nav-bar
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <div class="banner">
      <img src="../assets/logo.1c7ea124.png" alt="" />
    </div>
    <van-form>
      <van-field
        v-model="ruleForm.username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="ruleForm.pass"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 30px 15px">
        <van-button
          :style="[
            'border-color:#fb2020 ',
            {
              background:
                this.ruleForm.username && this.ruleForm.pass
                  ? '#fb2020'
                  : 'rgb(255,153,149)',
            },
          ]"
          round
          block
          type="info"
          native-type="submit"
          @click="handleSubmit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from "../api/ddk-reg";
export default {
  data() {
    return {
      ruleForm: {
        username: "张三丰",
        pass: "123123",
      },
      target:'/'
    };
  },
  created () {
    this.target=this.$route.query.target;
    console.log(this.target);
    
  },
  methods: {
    handleSubmit() {
      if (this.ruleForm.username && this.ruleForm.pass) {
        login(this.ruleForm).then((res) => {
          console.log(res);
          let { token, userInfo } = res.data.data;
          this.$store.commit("ddk/initUserInfoMut", {
            ...userInfo,
            token,
          });
          if(res.data.code==1){
            this.$router.push(this.target)
          }
        });
      } else {
        console.log("error submit!!");
        return false;
      }
    },
    onClickLeft() {
      this.$router.push("/reg");
    },
    onClickRight() {
      Toast("注册");
    },
  },
};
</script>

<style lang="less" scoped>
.van-button--round {
  border-radius: 0%;
  border-color: #fb2020;
}
.banner {
  height: 129px;
  text-align: center;
  line-height: 162px;
  img {
    height: 45px;
    width: 120px;
  }
}
</style>